package com.itsu.app.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.itsu.app.service.AppUserInfoService;
import com.itsu.app.service.AppUserService;
import com.itsu.app.service.AppUsersService;
import com.itsu.interfaces.entity.UserEntity;
import com.itsu.interfaces.entity.UserInfoEntity;
import com.itsu.interfaces.entity.UserLogin;
import com.itsu.interfaces.service.UserLoginService;
import com.itsu.pojo.constants.Constants;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.Set;

@EnableScheduling
@Configuration
public class TaskTimerConfig {

    private final Logger logger = LoggerFactory.getLogger(TaskTimerConfig.class);

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Autowired
    AppUserService appUserService;

    @Autowired
    AppUserInfoService appUserInfoService;

    @Reference
    UserLoginService userLoginService;

    static String L1[]  = {"北京", "天津", "河北", "山西", "内蒙", "辽宁", "吉林", "黑龙江",
            "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "台湾", "河南", "湖北", "湖南",
            "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西",
            "甘肃", "青海", "宁夏", "新疆"};

    static String L2[] = {"教育行业", "文娱行业", "制造行业", "计算机行业", "服务行业", "医疗行业"};


    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void reSoundLimit() {
        Set<String> keys = redisTemplate.keys(Constants.SOUND_UPPER_LIMIT + "*");
        System.out.println(keys);

        //判断key是否存在
        if (ObjectUtil.isEmpty(keys)) {
            logger.info("redis内无相关key需重置");
            return;
        }

        //先判断用户是否在线
        //true 执行下列代码
        for (String key : keys) {
            redisTemplate.opsForValue().set(key, "10");
            logger.info("redis内传音次数的相关key已重置");
        }

        //false
        /**
         * for (String key : keys) {
         *  redisTemplate.delete(key);
         *  logger.info("redis内传音次数的相关key已删除");
         * }
         */


    }

    //定时添加新用户
    @Scheduled(cron = "0 15 9 * * ?")
    public void createNewUser() {
        for (int i = 0; i < RandomUtil.randomInt(5); i++) {
            UserEntity userEntity = new UserEntity();
            userEntity.setMobile(RandomUtil.randomNumbers(11));
            userEntity.setPassword(RandomUtil.randomString(6));
            userEntity.setCreated(new Date());
            UserEntity user = appUserService.save(userEntity);

            UserInfoEntity userInfoEntity = new UserInfoEntity();
            userInfoEntity.setUserId(user.getId());
            userInfoEntity.setNickName("heima_" + user.getId());
            userInfoEntity.setLogo("http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/"+ RandomUtil.randomInt(50) +".jpg");
            userInfoEntity.setTags("单身,本科,年龄相仿");
            userInfoEntity.setSex(RandomUtil.randomInt(1, 3));
            userInfoEntity.setAge(RandomUtil.randomInt(18, 60));
            userInfoEntity.setEdu("本科");
            userInfoEntity.setCity(L1[RandomUtil.randomInt(32)]);
            userInfoEntity.setBirthday(DateUtil.formatDate(new Date()));
            userInfoEntity.setCoverPic("http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/"+ RandomUtil.randomInt(30)+".jpg");
            userInfoEntity.setIndustry(L2[RandomUtil.randomInt(6)]);
            userInfoEntity.setIncome(RandomUtil.randomNumbers(5));
            userInfoEntity.setMarriage("未婚");
            userInfoEntity.setSign(RandomUtil.randomNumbers(10));
            userInfoEntity.setCreated(new Date());
            userInfoEntity.setUpdated(new Date());
            appUserInfoService.save(userInfoEntity);


            UserLogin userLogin = new UserLogin();
            userLogin.setUserId(user.getId());
            userLogin.setLoginTime(new Date());
            userLogin.setOutTime(DateUtils.addHours(new Date(), 1));
            userLoginService.addUserLogin(userLogin);
        }
    }

}
